package com.zhou.bjgl.bjgl.business_web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhou.bjgl.bjgl.business_web.entity.BRole;
import com.zhou.bjgl.bjgl.business_web.entity.BUserRole;
import com.zhou.bjgl.bjgl.business_web.mapper.BUserRoleMapper;
import com.zhou.bjgl.bjgl.exception.BwException;
import com.zhou.bjgl.bjgl.exception.BwExceptionEnum;
import com.zhou.bjgl.bjgl.business_web.mapper.BRoleMapper;
import com.zhou.bjgl.bjgl.req.b_role.BRoleQueryReq;
import com.zhou.bjgl.bjgl.business_web.service.BRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.bjgl.bjgl.req.b_user_role.BUserRoleReq;
import com.zhou.bjgl.bjgl.resp.PageResp;
import com.zhou.bjgl.bjgl.resp.user_role.BUserRoleResp;
import com.zhou.bjgl.bjgl.util.CopyUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  权限servcie实现类
 * </p>
 *
 * @author jz
 * @since 2021-07-16
 */
@Service
public class BRoleServiceImpl extends ServiceImpl<BRoleMapper, BRole> implements BRoleService {

    @Resource
    private BRoleMapper bRoleMapper;

    @Resource
    private BUserRoleMapper bUserRoleMapper;

    /**
     * 添加权限
     * @param req 权限角色请求类
     */
    @Override
    public void add(BRoleQueryReq req) {
        BRole role = CopyUtil.copy(req, BRole.class);
        int insert = bRoleMapper.insert(role);
        if (insert == 0) {
            throw new BwException(BwExceptionEnum.INSERT_FAIL);
        }
    }

    /**
     * 给用户天权限
     * @param req   用户权限请求类
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertRoleMenu(BUserRoleReq req) {
        BUserRole bUserRole = CopyUtil.copy(req, BUserRole.class);
        bUserRoleMapper.insert(bUserRole);
    }

    @Override
    public PageResp<BUserRoleResp> listUserRoleList(BUserRoleReq req) {
        QueryWrapper<BUserRole> bUserRoleQueryWrapper = new QueryWrapper<>();
        if (req.getRoleId() == null) {
            PageHelper.startPage(req.getPage(),req.getSize());
            List<BUserRole> bUserRoles = bUserRoleMapper.selectList(bUserRoleQueryWrapper);
            PageInfo<BUserRole> bUserRolePageInfo = new PageInfo<>();
            List<BUserRoleResp> bUserRoleResps = CopyUtil.copyList(bUserRoles, BUserRoleResp.class);
            PageResp<BUserRoleResp> bUserRoleRespPageResp = new PageResp<>();
            bUserRoleRespPageResp.setPageList(bUserRoleResps);
            bUserRoleRespPageResp.setTotal(bUserRolePageInfo.getTotal());
            return bUserRoleRespPageResp;
        } else {
            //根据条件分页获取列表
            bUserRoleQueryWrapper.eq("user_id",req.getUserId());
            PageHelper.startPage(req.getPage(),req.getSize());
            List<BUserRole> bUserRolesById = bUserRoleMapper.selectList(bUserRoleQueryWrapper);

            //什么info获取分页后信息
            PageInfo<BUserRole> bUserRolePageInfo = new PageInfo<>();
            List<BUserRoleResp> bUserRoleResps = CopyUtil.copyList(bUserRolesById, BUserRoleResp.class);
            //创建返回对象
            PageResp<BUserRoleResp> bUserRoleRespPageResp = new PageResp<>();
            bUserRoleRespPageResp.setPageList(bUserRoleResps);
            bUserRoleRespPageResp.setTotal(bUserRolePageInfo.getTotal());

            return bUserRoleRespPageResp;
        }


    }

}
